package exam.test15;

import java.io.*;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2020-12-12
 * Time: 10:41
 */
public class LibraryItem implements Serializable {
    private String title;
    private String author;
    private int yearPublished;

    public LibraryItem(String title, String author, int yearPublished) {
        this.title = title;
        this.author = author;
        this.yearPublished = yearPublished;
    }

    public void processTextFile() {
        ArrayList<LibraryItem> libraryItems = new ArrayList<>();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader("items.txt"));) {
            int count = Integer.parseInt(bufferedReader.readLine());
            while (count > 0) {
                String title = bufferedReader.readLine().trim();
                String author = bufferedReader.readLine().trim();
                int yearPublished = Integer.parseInt(bufferedReader.readLine().trim());
                LibraryItem libraryItem = new LibraryItem(title, author, yearPublished);
                libraryItems.add(libraryItem);
                count--;
            }
        } catch (IOException e) {
            System.out.println("Read file data error");
            return;
        }

        try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("objects.dat"))) {
            os.writeObject(libraryItems);
        } catch (IOException e) {
            System.out.println("Write file data error");
        }
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public int getYearPublished() {
        return yearPublished;
    }
}
